<?php

namespace app\admin\controller\jy;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;

/**
 * 服务管理
 * @icon fa fa-circle-o
 */
class Service extends Backend
{
    /**
     * Service模型对象
     * @var \app\admin\model\jy\Service
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\jy\Service;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['servicetype'])
                ->where($where)
                ->where(['service.is_del' => 0])
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->getRelation('servicetype')->visible(['id', 'type_name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            Db::startTrans();
            try {
                $time = date("Y-m-d H:i:s", time());
                model('\app\admin\model\jy\Service')
                    ->whereIn('id', $ids)
                    ->update(['is_del' => 1, 'update_user' => $this->auth->id, 'update_time' => $time]);
                model('\app\admin\model\jy\ServicePavingType')
                    ->whereIn('service_id', $ids)
                    ->update(['is_del' => 1, 'update_user' => $this->auth->id, 'update_time' => $time]);
                Db::commit();
            } catch (PDOException | Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 获取操作模板
     * @return string
     */
    private function getSpecsTpl()
    {
        $inputStr = '<input id="c-paving_type" data-rule="required" class="form-control name-input" name="<%=name%>[<%=index%>][price]" type="number" maxlength="20"
                   placeholder="单位（元）" value="<%=row[\'price\']%>">';
        $id = 'paving_type_<%=index%>';
        $name = '<%=name%>[<%=index%>][pt_name]';
        $tpl = '<dd class="form-inline">'
            . $inputStr
            . '<input type="text" id="c-' . $id . '" name="' . $name . '" class="form-control pavingInput" value="<%=row[\'pt_name\']%>" size="25">'
            . '<div class="input-group-addon no-border no-padding form-control">'
            . '</div>'
            . '<span class="btn btn-sm btn-danger btn-remove pavingInput"><i class="fa fa-times"></i></span>'
            . '<span class="btn btn-sm btn-primary btn-dragsort pavingInput"><i class="fa fa-arrows"></i></span>'
            . '<span class="msg-box n-right" for="c-' . $name . '"></span>'
            . '<ul class="row list-inline plupload-preview" id="p-' . $id . '"></ul>'
            . '</dd>';
        return $tpl;
    }
}
